#include <iostream>
#include <mutex>
#include <stdexcept>
#include <thread>
static std::mutex mutex_;
static int race_num_;

void show_even(int i) {
    if(i % 2 == 0) {
        std::cout << "even : " << i << std::endl;
    }else{
        throw std::logic_error(" is not even");
    }
}

void print_thread_num(int i) {
    try
    {
        std::lock_guard<std::mutex> guard(mutex_);
        race_num_ = race_num_ + 1;
        show_even(race_num_);
    }
    catch(const std::logic_error& e)
    {
        std::cout << "exception !" << std::endl;
    }
    
}

int main()
{
    std::cout << "开始使用gurad:" << std::endl;
    const int thread_num = 10;
    std::thread threads[thread_num];
    for (int i = 0; i < thread_num; i++)
    {
        threads[i] = std::thread(print_thread_num, i + 1);
    }
    for(auto &t : threads) {
        t.join();
    }
    /* 输出的结果为:
            开始使用gurad:
            exception !
            even : 2
            even : 4
            exception !
            exception !
            even : 6
            exception !
            even : 8
            exception !
            even : 10
    */
}